package cn.zm.trip.web.controller;

import cn.hutool.core.collection.CollUtil;
import cn.zm.trip.web.commons.Msg;
import cn.zm.trip.web.dao.ViewPointDao;
import cn.zm.trip.web.domain.*;
import cn.zm.trip.web.service.ViewPointService;
import cn.zm.trip.web.service.WordsService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping(value = "/view")
public class ViewPointController {
	@Autowired
	private ViewPointService viewPointService;
	@Autowired
	private ViewPointDao viewPointDao;
	@Autowired
	private WordsService wordsService;


	/**
	 * 旅游景点跳转
	 * @return
	 */
	@RequestMapping(value = "point", method = RequestMethod.GET)
	public String viewPoint(ViewPointExample example, Model model,Integer page,String orderBy) {
		if (StringUtils.isBlank(orderBy)){
			orderBy = "heat";
		}
		List<ViewPoint> viewPoints = viewPointDao.listViewPointByQuery(null, page == null ? 0 : (page - 1) * 8, 8,orderBy);
		model.addAttribute("viewPoints", viewPoints);
		return "proscenium/viewpoint/view";
	}
	/**
	 * 景点内容 跳转
	 */
	@RequestMapping(value = "content", method = RequestMethod.GET)
	public String viewContent(Integer tpVid, Model model) {
		//封装留言信息
		List<Words> lw_list = wordsService.findByWords();
		model.addAttribute("lw_list",lw_list);

		//封装回复信息
		List<Reply> lr_list = wordsService.findByReply();
		model.addAttribute("lr_list",lr_list);

		//留言统计接口
		List<WordStatisticsEntity> wordStatisticsEntities = wordsService.listWordStaticsByWordQuery(tpVid, null,null);
		if (CollUtil.isNotEmpty(wordStatisticsEntities)) {
			int sum = wordStatisticsEntities.stream().mapToInt(WordStatisticsEntity::getQuantity).sum();

			for (WordStatisticsEntity statisticsEntity : wordStatisticsEntities) {
				Integer quantity = statisticsEntity.getQuantity();
				double radio = (quantity * 1.0) / sum;
				String proportion = (int) ((radio) * 100) + "%";
				statisticsEntity.setScoreTextByScore();
				statisticsEntity.setScoreRatioText(proportion);
			}
			model.addAttribute("wordStatics",wordStatisticsEntities);
		}

		ViewPoint viewPoint = viewPointService.selectByPrimaryKey(tpVid);
		viewPoint.setPictureList(JSON.parseArray(viewPoint.getPictureUrls(),String.class));

		//统计人气
		ViewPoint updateEntity = new ViewPoint();
		Integer heat = viewPoint.getHeat();
		updateEntity.setTpVid(viewPoint.getTpVid());
		updateEntity.setHeat(++heat);
		viewPointService.updateById(updateEntity);


		model.addAttribute("viewPoint", viewPoint);
		return "proscenium/viewpoint/content";
	}

	/**
	 * 前台景点模糊搜索
	 */
	@RequestMapping(value = "viewPointSearch", method = RequestMethod.GET)
	public String viewPointSearch(String keyword, Model model) {
		LambdaQueryWrapper<ViewPoint> queryWrapper = new LambdaQueryWrapper<>();
		if (StringUtils.isNotBlank(keyword)){
			queryWrapper.like(ViewPoint::getTpTitle,keyword)
				.or().like(ViewPoint::getTpLocation,keyword);
		}
		List<ViewPoint> viewPoints = viewPointService.list(queryWrapper);

		model.addAttribute("viewPoints", viewPoints);
		model.addAttribute("msg", Msg.success("景点查询成功!"));

		return "proscenium/viewpoint/view";
	}


}
